﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using CodeFirstMVC3.Domain.DataAccess;
using CodeFirstMVC3.Domain.Model;

namespace CodeFirstMVC3.DataAccess.Model
{
    internal class RegisterTypeMap
    {
        private Dictionary<Type, Type> _typeMap = new Dictionary<Type, Type>();

        public void Bind<TFor, TUse>()
        {
            _typeMap.Add(typeof(TFor), typeof(TUse));
        }

        private bool IsTypeMapped(Type type)
        {
            return _typeMap.ContainsKey(type);
        }

        public Type MapType(Type type)
        {
            if (IsTypeMapped(type))
                return _typeMap[type];

            return null;
        }
    }

    internal static class CodeFirstModelBinder
    {
        private static RegisterTypeMap _register = new RegisterTypeMap();

        static CodeFirstModelBinder()
        {
            _register.Bind<IMember, Member>();
            _register.Bind<IJob, Job>();
            _register.Bind<IRole, Role>();
            _register.Bind<IUser, User>();
        }

        public static Type Translate(this Type type)
        {
            return _register.MapType(type);
        }
    }

    public class CodeFirstDbContext : DbContext, IDbContext
    {
        public CodeFirstDbContext() : base("codefirstConnectionString") { }

        public DbSet<Member> Members { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Job> Jobs { get; set; }

        public new IDbSet<T> Set<T>() where T : class
        {
            return base.Set<T>();
        }

        public Database Query
        {
            get { return this.Database; }
        }

        public IDbSet<T> GetDbSet<T>() where T : class
        {
            return null;
        }
    }
}
